﻿using System;
using System.Reflection;
using System.IO;
using System.Diagnostics.Contracts;
using System.Diagnostics.CodeAnalysis;
namespace Common
{
    /// <summary>
    /// A utility class for accessing embedded resources
    /// </summary>
    public static class ResourceUtil
    {
        /// <summary>
        /// Fetches a stream of the specified resource file
        /// </summary>
        /// <param name="assembly">The assembly containing the resource</param>
        /// <param name="name">The name of the resource to fetch</param>
        /// <returns>A stream of the specified resource file, or null if not found</returns>
        [SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "1"), 
         SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "0")]
        public static Stream GetResourceStream(Assembly assembly, String name)
        {
            Contract.Requires(assembly != null);
            Contract.Requires(name != null);
            Contract.Requires(name.Length > 0);
            return assembly.GetManifestResourceStream(name);
        }
        /// <summary>
        /// Fetches the text of a specified resource file
        /// </summary>
        /// <param name="assembly">The assembly containing the resource</param>
        /// <param name="name">The name of the resource to fetch</param>
        /// <returns>The text of the resource, or null if not found</returns>
        [SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "1"), 
         SuppressMessage("Microsoft.Usage", "CA2202:Do not dispose objects multiple times")]
        public static String GetResourceAsText(Assembly assembly, String name)
        {
            Contract.Requires(assembly != null);
            Contract.Requires(name != null);
            Contract.Requires(name.Length > 0);
            using (Stream s = GetResourceStream(assembly, name))
            {
                if (s == null)
                { return null; }
                else
                {
                    using (StreamReader sr = new StreamReader(s))
                    { return sr.ReadToEnd(); }
                }
            }
        }
        /// <summary>
        /// Fetches a stream of the specified resource file from the calling assembly
        /// </summary>
        /// <param name="name">The name of the resource to fetch</param>
        /// <returns>A stream of the specified resource file, or null if not found</returns>
        [SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "0")]
        public static Stream GetResourceStream(String name)
        {
            Contract.Requires(name != null);
            Contract.Requires(name.Length > 0);
            return GetResourceStream(Assembly.GetCallingAssembly(), name);
        }
        /// <summary>
        /// Fetches the text of a specified resource file from the calling assembly
        /// </summary>
        /// <param name="name">The name of the resource to fetch</param>
        /// <returns>The text of the resource, or null if not found</returns>
        [SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "0")]
        public static String GetResourceAsText(String name)
        {
            Contract.Requires(name != null);
            Contract.Requires(name.Length > 0);
            return GetResourceAsText(Assembly.GetCallingAssembly(), name);
        }
    }
}
